//
//  HomeViewController.m
//  DefendHealth
//
//  Created by UNOHACHA on 17/2/18.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "HomeViewController.h"
#import "UserLoginVC.h"
#import "HomeEquipmentCell.h"

//#import "SelfEquipmVC.h"
//#import "MostDrilModelVC.h"
//#import "RunBeginVC.h"
//#import "CalculateClassroomVC.h"
//#import "DeviceConnectVC.h"
#import "SelfVitalityVC.h"


#import "LBXScanView.h"


#import "LBXScanResult.h"
#import "LBXScanWrapper.h"

#import "SubLBXScanViewController.h"

#import "AtWillRunVC.h"

#import "AllEquipmVC.h"
#import "PlanRunVC.h"
#import "AllDataVC.h"

#import "YRUNVC.h"

#import "SXMarquee.h"
#import "UIColor+Wonderful.h"
#import "HomeClassroomCell.h"


@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    SXMarquee *mar;
}

@property (nonatomic,strong) UITableView  *HomeTableView;
@end

@implementation HomeViewController


- (UITableView *)HomeTableView {
    
    if (!_HomeTableView) {
        
        _HomeTableView =  [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-k_TabBarHeight) style:UITableViewStyleGrouped];
        _HomeTableView.backgroundColor = k_TableBackColor;
        _HomeTableView.delegate = self;
        _HomeTableView.dataSource = self;
//        _HomeTableView.scrollsToTop = YES;
        _HomeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _HomeTableView.tableHeaderView = [self HeadViewShare];
//        _HomeTableView.mj_header =  [ MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(HomeDetailGet)];
        _HomeTableView.showsVerticalScrollIndicator = NO;

        _HomeTableView.tableHeaderView = [self HeadViewShare];
    }
    
    return _HomeTableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.Nav.hidden = YES;
    [self.view addSubview:self.HomeTableView];
    
     [self NavigationViewShard];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.f;
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//   
//    tableView.tableHeaderView = [self HeadViewShare];
//    
//    return [self sectionHeadView:section];
//}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return section == 1 ? 70.f:10.f;
    //return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 1) {
        
    UIView * footBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    footBgView.backgroundColor = k_TableBackColor;
        
    UIImageView * footImg = [ReduceCode creatImageVeiwWithFrame:CGRectMake(20, 10, kScreenWidth-40, 42) imageName:@"pic5" isCorner:NO];
    footImg.backgroundColor = UIColorFromRGB(0xcccaca);
    [footBgView addSubview:footImg];
    return footBgView;
    }
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    //return indexPath.section == 0 ? (82+6):130.f;
    return indexPath.section == 0 ? 44:197.f;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * cellidentif =  indexPath.section == 0 ? @"HomeEquipmentCell":@"HomeClassroomCell";
    
    //所有设备
    HomeEquipmentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentif];
        if (cell == nil) {
            cell = [[HomeEquipmentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentif];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    //我的设备
    HomeClassroomCell *Homecell = [tableView dequeueReusableCellWithIdentifier:cellidentif];
    if (Homecell == nil) {
        Homecell = [[HomeClassroomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentif];
        Homecell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    Homecell.indexBlock = ^(NSInteger index){
        
        [self pushAddDeviceVC];
        
        
        
    };
    
    return indexPath.section == 0 ? cell:Homecell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AllEquipmVC * selfEqVC =[AllEquipmVC new];
        //selfEqVC.titStr = @"亿健精灵跑步机1.0";
        [self.navigationController pushViewController:selfEqVC animated:YES];
    }else{
        PlanRunVC * selfEqVC =[PlanRunVC new];
      
        [self.navigationController pushViewController:selfEqVC animated:YES];
    }
    
   
}
//
//- (UIView *)sectionHeadView:(NSInteger)inde {
//    
//   
//    UIView * HeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
//    HeadView.backgroundColor = kWhiteColor;
//    
//    UILabel * lefTitL = [ReduceCode creatLabelWithFrame:CGRectMake(10, 0, 80, 14) title:@"" titleColor:UIColorFromRGB(0x333333) font:14 textAlignment:NSTextAlignmentLeft];
//    lefTitL.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
//    lefTitL.centerY = HeadView.centerY;
//    
//    [HeadView addSubview:lefTitL];
//    
//    UIButton * rigBtn = nil;
//    
//    
//    if (inde == 0) {
//        lefTitL.text = @"我的设备";
//         rigBtn = [UIButton creatBtnWithFrame:CGRectMake(kScreenWidth-90, 0, 80, 30) btnTitle:@"+添加设备" btnTitleColor:UIColorFromRGB(0x333333) btnTitleFont:14 btnTagPrice:0 btnSetImage:IMAGE(@"") btnBackgroundColor:nil isCorner:YES action:^(UIButton *button) {
//             [self pushAddDeviceVC];
//        }];
////        rigBtn.layer.cornerRadius = 15;
//        rigBtn.layer.borderWidth = 1;
//        rigBtn.layer.borderColor = K_LineColor.CGColor;
//        rigBtn.centerY = lefTitL.centerY;
//        UIView *hui = [UIView new];
//        hui.frame = CGRectMake(0, 49.5,WindowWidth , 0.5);
//        hui.backgroundColor = kLineColor;
//        [HeadView addSubview:hui];
//        
//    }else {
//         lefTitL.text = @"亿健课堂";
//        rigBtn = [UIButton creatBtnWithFrame:CGRectMake(kScreenWidth-60, 0, 50, 30) btnTitle:@"更多" btnTitleColor:UIColorFromRGB(0x333333) btnTitleFont:14 btnTagPrice:0 btnSetImage:IMAGE(@"point") btnBackgroundColor:nil isCorner:NO action:^(UIButton *button) {
//            
//        }];
//        [rigBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
//        rigBtn.centerY = lefTitL.centerY;
//    }
//    
//    
//    [HeadView addSubview:rigBtn];
//    return HeadView;
//}




- (UIView *)HeadViewShare {
    
    UIImageView * mainImg = [ReduceCode creatImageVeiwWithFrame:CGRectMake(0, 0, kScreenWidth, 280) imageName:@"bg1" isCorner:NO];
    mainImg.userInteractionEnabled = YES;
    
    
#pragma mark-头视图之左视图
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, k_NavigationHeight, kScreenWidth/2, 280-k_NavigationHeight)];
//    leftView.backgroundColor = kWhiteColor;
    [mainImg addSubview:leftView];
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth/2, k_NavigationHeight, kScreenWidth/2, 300-k_NavigationHeight)];
//    rightView.backgroundColor = kWhiteColor;
    [mainImg addSubview:rightView];
//    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 170)];
//    headerView.backgroundColor = kWhiteColor;
//    
//    [bigHeadView addSubview:headerView];
   
    UILabel * titL=  [ReduceCode creatLabelWithFrame:CGRectMake(20, 16, 80, 18) title:@"最近7天" titleColor:UIColorFromRGB(0x666666) font:18 textAlignment:NSTextAlignmentLeft];
    //titL.centerX = headerView.centerX;
    
    [leftView addSubview:titL];
    
    UILabel * runTimerL = [ReduceCode creatLabelWithFrame:CGRectMake(kScreenWidth/4, kMaxY(titL.frame)+20, kScreenWidth/2, 35) title:@"20.00" titleColor:kWhiteColor font:35 textAlignment:NSTextAlignmentCenter];
    runTimerL.transform = CGAffineTransformScale(runTimerL.transform, 0.9, 1.5);
    
    runTimerL.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:35];
    runTimerL.centerX = titL.centerX;
    
    [leftView addSubview:runTimerL];
    
    UIButton * vitalityBtn = [UIButton creatBtnWithFrame:CGRectMake(20, kMaxY(runTimerL.frame), kScreenWidth/2-20, 30) btnTitle:@"" btnTitleColor:UIColorFromRGB(0x666666) btnTitleFont:0 btnTagPrice:0 btnSetImage:nil btnBackgroundColor:nil isCorner:NO action:^(UIButton *button) {
        NSLog(@">>>>");
        [self.navigationController pushViewController:[AllDataVC new] animated:YES];
        
    }];
    [leftView addSubview:vitalityBtn];
    
    
    UILabel * vitalityL = [ReduceCode creatLabelWithFrame:CGRectMake(20, 0, 150, 17) title:@"运动时间/分钟" titleColor:kWhiteColor font:16 textAlignment:NSTextAlignmentLeft];
    
    vitalityL.centerY = vitalityBtn.centerY;
    
    [leftView addSubview:vitalityL];
    
    
    UIImageView * weather = [ReduceCode creatImageVeiwWithFrame:CGRectMake(kMaxX(vitalityBtn.frame)-10-7, 0, 7, 11) imageName:@"next" isCorner:NO];
    weather.centerY = vitalityL.centerY;
    [leftView addSubview:weather];
    
    [ToolUtil createLine:kMaxY(vitalityL.frame)+30 superView:leftView lift:20 width:kScreenWidth/2-20 hight:2 color:kWhiteColor];
//    UIButton * timerB = [UIButton creatBtnWithFrame:CGRectMake(kScreenWidth-10-27,0, 27, 27) btnTitle:@"" btnTitleColor:nil btnTitleFont:12 btnTagPrice:0 btnSetImage:IMAGE(@"clock") btnBackgroundColor:nil isCorner:NO action:^(UIButton *button) {
//        
//        
//    }];
//    timerB.centerY = titL.centerY;
//    [headerView addSubview:timerB];

    
    
    
//    //UILabel *runTimeL = []
//    UILabel * calorieL = [ReduceCode creatLabelWithFrame:CGRectMake(0, kMaxY(runTimerL.frame)-20, kScreenWidth/4, 20) title:@"3250" titleColor:UIColorFromRGB(0x333333) font:20 textAlignment:1];
//    calorieL.transform = CGAffineTransformScale(runTimerL.transform, 0.9, 0.9);
////    calorieL.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20];
//    //[headerView addSubview:calorieL];
//    
//    
//    UILabel * runNumL = [ReduceCode creatLabelWithFrame:CGRectMake(kScreenWidth/4*3, kMaxY(runTimerL.frame)-20, kScreenWidth/4, 20) title:@"5" titleColor:UIColorFromRGB(0x333333) font:20 textAlignment:1];
//    runNumL.transform = CGAffineTransformScale(runTimerL.transform, 0.9, 0.9);
////    runNumL.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20];
//    [headerView addSubview:runNumL];
//    
//    
////    CGFloat weight =(kScreenWidth-90*3)/4;
//    
//
    CGFloat width1 = kScreenWidth/2;
    CGFloat width2 = (width1-20)/2;
    NSArray * text = @[@"0 消耗大卡",@"0 运动次数"];
    //UILabel * l= nil;
    for (int U = 0; U<2; U++) {
        
        UILabel * runNumL = [ReduceCode creatLabelWithFrame:CGRectMake(20+width2*U, kMaxY(vitalityL.frame)+60, width2, 14) title:text[U] titleColor:kWhiteColor font:16 textAlignment:NSTextAlignmentLeft];
//        if (U == 1) {
//            runNumL.width = kScreenWidth/2;
//        }
//        if (U == 2) {
//            runNumL.x = kScreenWidth/4*3;
//        }
//        l = runNumL;
        [leftView addSubview:runNumL];
    }
#pragma mark-头视图之右视图
    UIImageView *rightImageView = [ReduceCode creatImageVeiwWithFrame:CGRectMake(30, 20, width1-60, 130) imageName:nil isCorner:NO];
    
    [rightView addSubview:rightImageView];
    
    UIButton *energyVBut = [UIButton creatBtnWithFrame:CGRectMake((width1-100)/2+5, kMaxY(rightImageView.frame)+25, 100, 22) btnTitle:@"活力值 100" btnTitleColor:kWhiteColor btnTitleFont:12 btnTagPrice:0 btnSetImage:nil btnBackgroundColor:UIColorFromRGB(0xe85f2b) isCorner:NO action:^(UIButton *button) {
        [self.navigationController pushViewController:[SelfVitalityVC new] animated:YES];                                
    }];
    //energyVBut.centerX = rightView.centerX;
    energyVBut.layer.cornerRadius = 10;
    energyVBut.clipsToBounds = YES;
    [rightView addSubview:energyVBut];
      
    return mainImg;
}





- (void)NavigationViewShard {
    
    UIView * navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, k_NavigationHeight)];
    [self.view addSubview:navView];
    

    UIButton * RECoder = [UIButton creatBtnWithFrame:CGRectMake(10,0, 30, 30) btnTitle:@"" btnTitleColor:nil btnTitleFont:12 btnTagPrice:0 btnSetImage:IMAGE(@"scanning") btnBackgroundColor:nil isCorner:NO action:^(UIButton *button) {
        [self coderSean];
    }];
    RECoder.centerY = navView.centerY+10;
    [navView addSubview:RECoder];
    
    UIButton * runBtn = [UIButton creatBtnWithFrame:CGRectMake(kScreenWidth-10-30, 0, 30, 30) btnTitle:@"" btnTitleColor:nil btnTitleFont:12 btnTagPrice:0 btnSetImage:IMAGE(@"people") btnBackgroundColor:nil isCorner:NO action:^(UIButton *button) {
        
        [self atWillRun];
        
    }];
    runBtn.centerY = navView.centerY+10;
    [navView addSubview:runBtn];
    
    
    
    
    
    
    mar = [[SXMarquee alloc]initWithFrame:CGRectMake(kMaxX(RECoder.frame)+10, 30, kScreenWidth-100, 20) speed:4 Msg:@"13°C | PM5.0 空气质量65 今天更适合室内跑步" bgColor:kWhiteColor txtColor:UIColorFromRGB(0x666666)];
    [mar changeMarqueeLabelFont:[UIFont systemFontOfSize:16]];
    [mar changeTapMarqueeAction:^{
        
    }];
    [mar start];
    [navView addSubview:mar];
    
    
    
    
    
//    UIImageView * weather = [ReduceCode creatImageVeiwWithFrame:CGRectMake(10, 0, 18, 18) imageName:@"sunlight" isCorner:YES];
////    weather.backgroundColor = [UIColor yellowColor];
//    weather.centerY = navView.centerY+10;
//    [navView addSubview:weather];
    
//    UILabel * temL = [ReduceCode creatLabelWithFrame:CGRectMake(kMaxX(weather.frame)+6, 0, 30, 20) title:@"13°" titleColor:UIColorFromRGB(0x333333) font:20 textAlignment:NSTextAlignmentLeft];
//    temL.centerY = navView.centerY+10;
//    [navView addSubview:temL];
    
//    UILabel * numL = [ReduceCode creatLabelWithFrame:CGRectMake(kMaxX(temL.frame)+4, temL.y, 20, 10) title:@"65" titleColor:UIColorFromRGB(0x333333) font:10 textAlignment:NSTextAlignmentLeft];
////    numL.centerY = navView.centerY;
//    [navView addSubview:numL];
    
//    UILabel * pmL = [ReduceCode creatLabelWithFrame:CGRectMake(kMaxX(temL.frame)+4, kMaxY(numL.frame)+2, 50, 10) title:@"P.M 5.0" titleColor:UIColorFromRGB(0x333333) font:10 textAlignment:NSTextAlignmentLeft];
//    [navView addSubview:pmL];
    
    
    
//    UIButton * centenBtn = [UIButton creatBtnWithFrame:CGRectMake(0, 20, 50, 44) btnTitle:@"智能管家\n卡通形象" btnTitleColor:UIColorFromRGB(0xffffff) btnTitleFont:11 btnTagPrice:0 btnSetImage:nil btnBackgroundColor:UIColorFromRGB(0x666666) isCorner:NO action:^(UIButton *button) {
//        
//        
//    }];
//    centenBtn.titleLabel.numberOfLines = 0;
//    centenBtn.centerX = navView.centerX;
//    centenBtn.alpha = 0.8;
//    [navView addSubview:centenBtn];
    

//    UIView *hui = [UIView new];
//    hui.frame = CGRectMake(0, 63.5,WindowWidth , 0.5);
//    hui.backgroundColor = kLineColor;
//    [navView addSubview:hui];
    
}


#pragma mark -- 地图随意跑

- (void)atWillRun {
    
    
    AtWillRunVC * vc  = [[AtWillRunVC alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    
}

#pragma mark -- 二维码
- (void)coderSean {
    
    
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 60;
    style.xScanRetangleOffset = 30;
    
    if ([UIScreen mainScreen].bounds.size.height <= 480 )
    {
        //3.5inch 显示的扫码缩小
        style.centerUpOffset = 40;
        style.xScanRetangleOffset = 20;
    }
    
  
//    style.alpa_notRecoginitonArea = 0.6;
    
      //内嵌，一般不显示矩形框情况下
//    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
     //扫码框周围4个角绘制线宽度
      style.photoframeLineW = 0.0;
//    码框周围4个角的颜色
//       style.colorAngle = UIColorFromRGB(0x333333);
//     //扫码框周围4个角的宽度
//    style.photoframeAngleW = 16;
//    //扫码框周围4个角的高度
//    style.photoframeAngleH = 16;
    
    //显示矩形框
    style.isNeedShowRetangle = NO;
    //动画类型：线条上下移动
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    //矩形框颜色
    style.colorRetangleLine = [UIColor colorWithRed:247/255. green:202./255. blue:15./255. alpha:1.0];

    
    //使用的自定义图片
    UIImage *imgFullNet = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_light_green @2x"];
    
    
    style.animationImage = imgFullNet;

    
    SubLBXScanViewController *vc = [SubLBXScanViewController new];
    vc.style = style;
    //vc.isOpenInterestRect = YES;
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - - 添加设备
-(void)pushAddDeviceVC{
    
    YRUNVC *YRunVC = [[YRUNVC alloc]init];
    
    [self.navigationController pushViewController:YRunVC animated:YES];

//    AllEquipmVC *allEqVC = [[AllEquipmVC alloc]init];
//    [self presentViewController:allEqVC animated:YES completion:nil];
//#endif
   
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [mar start];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
